<?php namespace app\api\controller;

use think\Controller;
use think\Db;

class Auth extends  Controller{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function getAccessToken(){
		if(request() -> isGet()){
			$in = input("get.");
			$bool = is_empty(['appId', 'appSecret'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$where['a.userName|b.ContactPhone'] = $in['appId'];
				$where['a.userPwd'] = md5(md5($in['appSecret']) . config("ERP_AUTH_KEY"));
				$where['a.isDelete'] = 'N';
				$where['b.isDelete'] = 'N';
				$where['field'] = ['a.ID', 'a.SysID', 'a.userName', 'a.IsManager', 'a.UserPwd', 'a.Enable', 'b.Type', 'b.ContactPhone'];
				$userInfo = controller("service/SysUser") -> getJoinWmsWarehouseuserGetSingleData($where);
				if( $userInfo['code'] != '1' ){
					returnResults(['code'=>'40014']);
				}
//				var_dump(Db::getLastSql());
//				p($userInfo);die;
				
				$districtCof = config("ZX_DISTRICT_ID");
				$districtIn['districtId'] = $districtCof;
				$districtInfo = controller("service/District") -> getSingleData($districtIn);
				
//				记录登录日志
				$newLoginLog['userId'] = $userInfo['info']['SysID'];
				$newLoginLog['userName'] = $userInfo['info']['userName'];
				$newLoginLog['loginStatus'] = '1';
				$newLoginLog['platform'] = 'fazaitiaojie';
				controller("service/LogLogin") -> addData($newLoginLog);
				
//				获取token
				$loginTime = config('OPEN_LOGIN_EXPIRE_TIME');
				$getTokenData['loginType'] = 2;
				$getTokenData['id'] = $userInfo['info']['SysID'];
				$getTokenData['sysId'] = $userInfo['info']['SysID'];
				$getTokenData['pwd'] = $userInfo['info']['UserPwd'];
				$getTokenData['loginTime'] = $loginTime;
				$loginToken = controller("common/Token") -> setToken($getTokenData);
			
				$outArr['info']['accessToken'] = $loginToken;
				$outArr['info']['expiresIn'] = $loginTime;
				$outArr['code'] = '1';
				
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>